<?php

class Sys_SysDictCatController extends Zend_Controller_Action
{

	private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();
		
		$this->common->checklogin(true, true);

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Base();
        /* Initialize action controller here */
    }

	public function getlistAction()
	{

		$start = $this->request->getParam("start");
		$limit = $this->request->getParam("limit");
		$_iAmount = $this->db->fetchOne("SELECT count(*) FROM sysdictcat WHERE status & 1 = 0");
		$result = $this->db->fetchAll("SELECT * FROM sysdictcat WHERE status & 1 = 0 limit $start, $limit");
		$sMsg = "";
		$len = count($result);
		for($i=0; $i < $len; $i++) {
			$comma = ",";
			if($i == ($len -1)) 
			{
				$comma = "";
			}
			$sMsg .= "{DictCatID:" . $result[$i]["DictCatID"] . ",DictCatName:'" . $result[$i]["DictCatName"] . "',DictCatNameLink:'" . $result[$i]["DictCatName"] . "',ObjName:'" . $result[$i]["ObjName"] . "', ObjType:" . $result[$i]["ObjType"] . ", AreaType: " . $result[$i]["AreaType"] . ",ParentID:'" . $result[$i]["ParentID"] . "',SonNode:'" . $result[$i]['SonNode'] . "',SonNodeLink:'" . $result[$i]["SonNode"] . "',Depth:'" . $result[$i]["Depth"] . "',IsCache:'" . $result[$i]["IsCache"] . "',OptionSet:'" . $result[$i]["OptionSet"] . "',Remark:'" . $result[$i]["Remark"] . "', CreateOn:'" . $this->common->getLocaldate($result[$i]["CreateOn"]) . "', Status:" . $result[$i]["Status"] . " }" . $comma;
		}

		$acl = array("aRead" => 1, "aNew" => 1, "aEdit" => 1, "aLock" => 1, "aDel" => 1, "aList" => 1);
		$_sExtJSD = "Acl:[{aRead:" . $acl["aRead"] . ", aNew:" . $acl["aNew"] . ",aEdit:" . $acl["aEdit"] . ",aDel:" . $acl["aDel"] .
							  ",aList:" . $acl["aList"] . "}]";
		$this->common->PutData('{totalProperty:' . $_iAmount . ',records:[' . $sMsg . '],' . $_sExtJSD . '}');
		exit;
	}

	public function saveAction()
	{
			$DictCatID = $this->request->getParam("DictCatID");
			$DictCatName = $this->request->getParam("DictCatName");
			$ObjName = $this->request->getParam("ObjName");
			$ObjType = $this->request->getParam("ObjType");
			$AreaType = $this->request->getParam("AreaType");
			$AreaType = 0;
			$ParentID = $this->request->getParam("ParentID");
			$ParentID = 0;
			$Remark = $this->request->getParam("Remark");
			$IsCache = $this->request->getParam("IsCache");
			$userid = $this->common->getUserid();

		if($DictCatID > 0) {
			if($this->db->update("sysdictcat", array(
					"DictCatName" => $DictCatName,
					"ObjName"    => $ObjName,
					"ObjType"		  => $ObjType,
					"AreaType"		=> $AreaType,
					"ParentID"    => $ParentID,
					"Remark"		=> $Remark,
					"IsCache"		=> $IsCache
				), $this->db->quoteInto('DictCatID = ?', $DictCatID)))
				{
					$this->common->PutData('{result:""}');
				}
				else
				{
					$this->common->PutData('{error:"保存失败!"}');
				}
		}
		else
		{
			if($this->db->insert("sysdictcat", array(
				"DictCatName" => $DictCatName,
				"ObjName"	  => $ObjName,
				"ObjType"     => $ObjType,
				"AreaType"	  => 0,
				"ParentID"	=> 0,
				"SonNode"   => 0,
				"Depth"		=> 0,
				"Remark"	=> $Remark,
				"OptionSet"	=> 0,
				"CreateOn"  => $this->common->getGmtime(),
				"Status"	=> 0,
				"IsCache"	=> $IsCache
			)) >= 0) {
				
				$this->common->PutData('{result:""}');
			}
			else
			{
				$this->common->PutData('{error:"保存失败!"}');
			}
		}

		exit;
	}

	public function delAction()
	{
		$uids = $this->request->getParam("aid");
		
		if($this->db->update("sysdictcat", array(
					"status" => 1
				), $this->db->quoteInto('DictCatID in (?)', $uids))) 
		{
			
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

	public function lockAction()
	{
		$uids = $this->request->getParam("aid");
		
		$stmt = $this->db->query("UPDATE sysdictcat SET status = 2 - status & 2 WHERE status in (0,2) AND " . $this->db->quoteInto('DictCatID in (?)', $uids));
		if($stmt->rowCount())
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}
}